//
//  TuiHuanCauseView.m
//  Experss
//
//  Created by wmwMAC on 17/8/9.
//  Copyright © 2017年 嗯，大葱. All rights reserved.
//

#import "TuiHuanCauseView.h"


static NSString *identifier = @"scheduleIdentifiercell";

@interface TuiHuanCauseView ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;


@end

@implementation TuiHuanCauseView
-(void)setCauseArr:(NSArray *)causeArr{
    _causeArr = causeArr;
    
    if (_causeArr.count) {
        [self addTableviewUI];
    }
}

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        
    }
    return self;
}

- (void)addTableviewUI {
    
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    backBtn.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
    backBtn.backgroundColor = [UIColor blackColor];
    backBtn.alpha = 0.3;
    [backBtn addTarget:self action:@selector(backBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:backBtn];
    
    CGFloat tableview_height = self.causeArr.count*50;
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(50, (kScreenHeight-tableview_height-50)/2, kScreenWidth-100, tableview_height+50) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 50;
    self.tableView.backgroundColor = BaseColor;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:identifier];
    [self addSubview:self.tableView];
}
-(void)backBtnClick{
    [self removeFromSuperview];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.causeArr.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
    
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    cell.textLabel.text = _causeArr[indexPath.row];
    cell.textLabel.font = [UIFont systemFontOfSize:14];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 50;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headerView = [[UIView alloc] init];
    headerView.backgroundColor = [UIColor whiteColor];
    
    UILabel *titleLable = [BasePublicCreatUI createLabelFrame:CGRectMake(0, 0, kScreenWidth-50, 50) title:@"退货原因" textColor:BaseBlueColor textAlignment:NSTextAlignmentCenter font:[UIFont systemFontOfSize:16]];
    [headerView addSubview:titleLable];
    
    return headerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.001;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *footview = [[UIView alloc] init];
    footview.backgroundColor = BaseColor;
    
    return footview;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self.delegate tuiHuanCause:_causeArr[indexPath.row] CauseId:[NSString stringWithFormat:@"%ld",indexPath.row]];
    [self removeFromSuperview];
}


@end
